package ai4games_tests.tests;

import game2dai.World;
import game2dai.entities.Vehicle;
import game2dai.entities.Wall;
import game2dai.entityshapes.Picture;
import game2dai.entityshapes.ps.ArrowPic;
import game2dai.entityshapes.ps.Hints;
import game2dai.entityshapes.ps.WallPic;
import game2dai.maths.Vector2D;
import game2dai.steering.AutoPilot;
import game2dai.steering.SBF;
import processing.core.PApplet;


/**
 * Testing wall avoidance
 * @author Peter
 *
 */
public class GameTest_007 extends PApplet {

	final boolean HINTS = true;

	World w;
	Wall wall0 = null, wall1 = null;
	WallPic wpic = null;
	Picture view;

	Vehicle v;
	AutoPilot sb;

	Vector2D home;
	
	long ct, et, lt;
	float deltaTime;

	public void setup(){
		size(600,600);
		w = new World(width, height);

		home = new Vector2D(width - 100, height - 176);

		wall0 = new Wall(new Vector2D(120,10), new Vector2D(300, height - 170), true);
		wpic = new WallPic(this);
//		wpic.setShowHints(HINTS);
		wall0.renderer(wpic);
		w.add(wall0);
		
		wall1 = new Wall(new Vector2D(300, height - 170), new Vector2D(400, height - 170), true);
		wpic = new WallPic(this);
//		wpic.setShowHints(HINTS);
		wall1.renderer(wpic);
		w.add(wall1);
		
		sb = new AutoPilot();
//		sb.enableBehaviours(SBF.ARRIVE | SBF.WALL_AVOID);
		sb.arriveOn(home).wallAvoidOn();
//		sb.setTarget(SBF.ARRIVE, home);

		v = new Vehicle(new Vector2D(10, 10), // position
				20, // collision radius
				new Vector2D(50,0), // velocity
				50, // maximum speed
				new Vector2D(1,1), // heading
				1, // mass
				1, // turning rate
				200 // max force
				); 
		view = new ArrowPic(this);	
		view.showHints(Hints.HINT_HEADING | Hints.HINT_VELOCITY);
		v.renderer(view);
		v.AP(sb);

		w.add(v);
		
		ct = lt = millis();	
	}

	public void draw(){
		ct = millis();
		et = ct - lt;
		lt = ct;
		deltaTime = et / 1000.0f;

		background(220,255,220);

		fill(0,64,0);
		noStroke();
		ellipseMode(CENTER);
		ellipse((float)home.x, (float)home.y, 14,14);

		w.update(deltaTime);
		w.draw();
		
		drawWallNormal(wall0);
		drawWallNormal(wall1);
	}

	public void drawWallNormal(Wall wall){
		Vector2D s = wall.getStartPos();
		Vector2D e = wall.getEndPos();
		Vector2D m = Vector2D.add(s, e);
		m.div(2);
		Vector2D np = new Vector2D(wall.norm());
		np.mult(40);
		np.add(m);
		stroke(0,0,255);
		line((float)m.x, (float)m.y, (float)np.x, (float)np.y);
		
	}
}
